import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// import '../models/policy.dart';
import '../providers/policy.dart';

// final pageProvider = StateProvider<List<Policy>>((ref) => []);

class PolicyPage extends ConsumerWidget {
  const PolicyPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // final list = ref.watch(pageProvider);
    final policies = ref.watch(policiesProvider());
    final dictionaries = ref.read(policyDictionaryProvider("sex"));
    return Scaffold(
      appBar: AppBar(
        title: const Text('政策列表'),
      ),
      body: Center(
        child: policies.when(
          data: (value) {
            // ref.read(pageProvider.notifier).state = [...list, ...value];
            return ListView.builder(
              itemCount: value.length,
              itemBuilder: (context, index) {
                return GestureDetector(
                  onDoubleTap: () async {
                    Map<String, dynamic> requestBody = {
                      "enterpriseCode": "91422300MACY6K6N3K",
                      "enterpriseName": "湖北省灵醒云科技有限公司",
                      "handlingArea": "咸宁市",
                      "department": value[index].createUserDeptName,
                      "opinion": value[index].name,
                      "description": value[index].subsidyStandards
                    };
                    var result = await ref
                        .read(proposalCreateProvider(requestBody).future);
                    BotToast.showText(text: result.msg);
                  },
                  child: ListTile(
                    title: Text(value[index].name ?? ""),
                    subtitle: Text(value[index].subsidyStandards ?? ""),
                  ),
                );
              },
            );
          },
          error: (error, stack) => Text('Error: ${error.toString()}'),
          loading: () => const CircularProgressIndicator(),
        ),
      ),
    );
  }
}
